package com.hilaver.dzmis.web.servlet.procurement;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.procurement.ProductPackage;
import com.hilaver.dzmis.procurement.ProductPackageItem;
import com.hilaver.dzmis.service.impl.BiFranceCustomerServiceImpl;
import com.hilaver.dzmis.service.impl.ProductPackageServiceImpl;
import com.hilaver.dzmis.util.StringUtils;

public class ProductPackageServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	public static final int ACTION_EDIT_ITEM = 100;
	
	public static final int ACTION_GET_ITEM_ALL = 101;
	
	public static final int ACTION_DELETE_ITEM = 102;
	
	public static final int ACTION_CALCULATE = 103;
	
	public static final int ACTION_EDIT_PGI = 200;
	
	public static final int ACTION_GET_PGI_ALL = 201;
	
	public static final int ACTION_DELETE_PGI = 202;
	
	public static final int ACTION_GET_PGI = 203;

	private ProductPackageServiceImpl pgService;
	
	private BiFranceCustomerServiceImpl fcService;
	
	private ProductPackage pg;
	
	private ProductPackageItem ppi;
	
	public ProductPackageServlet() {
		super();
		this.simpleName = ProductPackage.class.getSimpleName();
		this.fullName = ProductPackage.class.getName();
		this.pgService = new ProductPackageServiceImpl();
		this.fcService = new BiFranceCustomerServiceImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.pgService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));

		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.pgService.edit(this.pg);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.pgService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.pgService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setId(request);
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = this.pgService.getAllPagination(this.page,
					this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.pgService.getAll(this.id);
			break;
		case ACTION_EDIT_ITEM:
			this.setId(request);
			this.fillIds(request);
			rtn = this.pgService.editItem(this.id, this.ids);
			break;
		case ACTION_GET_ITEM_ALL:
			this.setId(request);
			rtn = this.pgService.getItemAll(this.id);
			break;
		case ACTION_DELETE_ITEM:
			this.setId(request);
			rtn = this.pgService.deleteItem(this.id);
			break;
		case ACTION_CALCULATE:
			this.setId(request);
			rtn = this.pgService.orderPackage(this.id);
			break;
		case ACTION_EDIT_PGI:
			this.fillProductPackageItem(request);
			rtn = this.pgService.editPackageItem(this.ppi);
			break;
		case ACTION_GET_PGI_ALL:
			this.setId(request);
			rtn = this.pgService.getPackageItemAll(this.id);
			break;
		case ACTION_GET_PGI:
			this.setId(request);
			rtn = this.pgService.getPackageItem(this.id);
			break;
		case ACTION_DELETE_PGI:
			this.setId(request);
			rtn = this.pgService.deletePackageItem(this.id);
			break;
		}

		return rtn;
	}
	
	private void fillObj(HttpServletRequest request) throws Exception{
		super.fillObj(request);
		int customerId = Integer.parseInt(request.getParameter("customerID"));
		this.pg = (ProductPackage)this.obj;
		this.pg.setBiFranceCustomer(this.fcService.getObj(customerId));
	}
	
	private void fillProductPackageItem(HttpServletRequest request) throws Exception{
		this.setId(request);
		this.ppi = new ProductPackageItem();
		this.ppi.setId(this.id);
		ProductPackage pp = new ProductPackage();
		pp.setId(Integer.parseInt(request.getParameter("ppId")));
		this.ppi.setProductPackage(pp);
		this.ppi.setOrderNumber(request.getParameter("orderNumber"));
		this.ppi.setReference(request.getParameter("reference"));
		this.ppi.setYarnName(request.getParameter("yarnName"));
		this.ppi.setColor(request.getParameter("color"));
		this.ppi.setPackageNumber(Integer.parseInt(request.getParameter("packageNumber")));
		this.ppi.setChoice(request.getParameter("choice"));
		this.ppi.setStandard(request.getParameter("standard"));
		Integer total = 0;
		if(!StringUtils.isEmpty(request.getParameter("amountS"))){
			this.ppi.setAmountS(Integer.parseInt(request.getParameter("amountS")));
			total += this.ppi.getAmountS();
		}else{
			this.ppi.setAmountS(null);
		}
		if(!StringUtils.isEmpty(request.getParameter("amountM"))){
			this.ppi.setAmountM(Integer.parseInt(request.getParameter("amountM")));
			total += this.ppi.getAmountM();
		}else{
			this.ppi.setAmountM(null);
		}
		if(!StringUtils.isEmpty(request.getParameter("amountL"))){
			this.ppi.setAmountL(Integer.parseInt(request.getParameter("amountL")));
			total += this.ppi.getAmountL();
		}else{
			this.ppi.setAmountL(null);
		}
		if(!StringUtils.isEmpty(request.getParameter("amountXl"))){
			this.ppi.setAmountXl(Integer.parseInt(request.getParameter("amountXl")));
			total += this.ppi.getAmountXl();
		}else{
			this.ppi.setAmountXl(null);
		}
		if(!StringUtils.isEmpty(request.getParameter("amountXxl"))){
			this.ppi.setAmountXxl(Integer.parseInt(request.getParameter("amountXxl")));
			total += this.ppi.getAmountXxl();
		}else{
			this.ppi.setAmountXxl(null);
		}
		if(!StringUtils.isEmpty(request.getParameter("amountXxxl"))){
			this.ppi.setAmountXxxl(Integer.parseInt(request.getParameter("amountXxxl")));
			total += this.ppi.getAmountXxxl();
		}else{
			this.ppi.setAmountXxxl(null);
		}
		this.ppi.setPackageAmount(total);
	}
}
